#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include "PID.h"
#include "main.h"
#include "adc.h"
#include "dma.h"
#include "i2c.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"
#include "RDdata.h"
#include "OLED.h"
#include "Key.h"
#include "RDdata.h"
#include "Control.h"
/*BianLiang ShengMing Begin*/
uint8_t msgT[64];
uint8_t msgR[64];
uint8_t set;
float Trace_Bias = 0;
uint8_t time_slice = 0;
uint8_t Finsh_Flag=1;
/*BianLiang ShengMing End*/

void setup(){
    HAL_UART_Transmit(&huart1,(uint8_t*)"hello",sizeof("hello"),100);
    HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_1);
    HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_2);
    HAL_TIM_Base_Start_IT(&htim1);
    JY901_RDDat(&IMU);
    Trace_Bias=IMU.Angle[2];
    PID_Init();
    Question_Init();
    worning(0);
    HAL_Delay(1000);
}

void loop() {
    if (key_waiting) {
        HAL_Delay(40);
        key_waiting = 0;
    }
    if(Finsh_Flag) {
        switch(key_flag){
        case 1:
            Trace_Black_Flag=1;
            key_flag=0;
            Finsh_Flag=0;
        break;
        case 2:
            Forward_Flag=1;
            key_flag=0;
            Finsh_Flag=0;
        break;
        case 3:
            Around_Trace_Flag=1;
            key_flag=0;
            Finsh_Flag=0;
        break;
        case 4:
            Cross_Half_Flag=1;
            key_flag=0;
            Finsh_Flag=0;
        break;
        case 5:
            Around_Trace_Two_Flag=1;
            key_flag=0;
            Finsh_Flag=0;
        break;
        case 6:
            Cross_Full_Flag=1;
            key_flag=0;
            Finsh_Flag=0;
        break;
        }
    }

    HAL_Delay(10);
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
    if(htim->Instance==TIM1) {
        if(Cross_Half_Flag!=0) {
            Cross_half();
        }
        else if(Trace_Black_Flag!=0) {
            Trace_Black();
        }
        else if(Around_Trace_Flag!=0) {
            Around_Trace();
        }
        else if(Around_Trace_Two_Flag!=0) {
            Around_Trace_Two();
        }
        else if(Forward_Flag!=0) {
            Forward_Trace();
        }
        else if(Cross_Full_Flag!=0) {
            Cross_Full();
        }
        /*if(Forward_Flag!=0) {
            Forward_Trace();
        }*/
        /*if(Cross_Full_Flag!=0) {
            Cross_Full();
        }*/
    }
}



